﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using ASS.BUS.DTO;
using ASS.BUS.Services;
using ASS.DAO;

namespace ASS
{
    public partial class FrmTreatments : Form
    {
        NhanVien _nhanVien;
        public FrmTreatments(FrmMainClinicStaff frmMain, NhanVien nhanVien)
        {
            _nhanVien = nhanVien;
            _frmMain = frmMain;
            InitializeComponent();
        }

        private BenhNhan _benhNhan;
        private BenhNhanServices _benhNhanServices = new BenhNhanServices();
        private ThuocServices _thuocServices = new ThuocServices();
        private Thuoc _thuoc = new Thuoc();
        private DonThuoc _donThuoc = new DonThuoc();
        private DonThuocServices _donThuocServices = new DonThuocServices();
        private FrmMainClinicStaff _frmMain;
        private void rdbTaiKham_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTaiKham.Checked == true)
            {
                dtpNgayTaiKham.Visible = true;
            }
        }

        private void FrmTreatments_Load(object sender, EventArgs e)
        {
            lblBacsi.Text = "Bác sĩ, " + _nhanVien.Ten;
            var benhNhans = _benhNhanServices.GetBenhNhans();
            var banhNhanList = benhNhans.Select(benhNhan => benhNhan.MaBn).ToList();
            cboPatientID.DataSource = banhNhanList;

            var thuocs = _thuocServices.GetThuocs();
            var thuocList = thuocs.Select(thuoc => thuoc.Ten).ToList();
            cboNameStock.DataSource = thuocList;

            lvwPatientHistory.Columns[0].DisplayIndex = 0;
            lvwPatientHistory.Columns[1].DisplayIndex = 1;
            lvwPatientHistory.Columns[2].DisplayIndex = 2;
            lvwPatientHistory.Columns[3].DisplayIndex = 3;
        }

        private void cboPatientID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboPatientID.SelectedIndex == -1) return;           
            var id = Int32.Parse(cboPatientID.SelectedValue.ToString());
            _benhNhan = _benhNhanServices.GetBenhNhan(id);

            if (_benhNhan != null)
            {
                txtThongTinBn.Text = @"Họ tên: " + _benhNhan.Ten + "\n" + @"Địa chỉ: " + _benhNhan.DiaChi + "\n" +
                                     @"Ngày sinh: " + _benhNhan.NgaySinh + "\n" + @"Tình trạng: " + _benhNhan.MucDo;
    
            }
            lvwThuoc.Items.Clear();
            lvwPatientHistory.Items.Clear();
            var donThuocs = _donThuocServices.GetDonThuocs().Where(x => x.MaBn == _benhNhan.MaBn).ToList();
            foreach (var donThuoc in donThuocs)
            {
                string[] row = { donThuoc.MaToa.ToString() ,donThuoc.NgayKham, donThuoc.ChuanDoan, donThuoc.DanDo };
                var item = new ListViewItem(row);
                lvwPatientHistory.Items.Add(item);
            }
        }

        private void btnThemThuoc_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text == null) return;
            
            string[] row = { _thuoc.Ten, rtfLieuDung.Text, txtSoLuong.Text };
            var listViewItem = new ListViewItem(row);
            lvwThuoc.Items.Add(listViewItem);
        }

        private void cboNameStock_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboNameStock.SelectedIndex == -1) return;
            var tenThuoc = cboNameStock.SelectedValue.ToString();
            _thuoc = _thuocServices.GetThuoc(tenThuoc);
           
            if (_thuoc != null)
            {
                txtDonVi.Text = _thuoc.DonVi;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var donThuoc = new DonThuoc();
            var donThuocServices = new DonThuocServices();
            var soLuongList = lvwThuoc.Items.Cast<ListViewItem>().Select(l => l.SubItems[2].Text).Where(text => text != null).ToList();
            var tenThuocList = lvwThuoc.Items.Cast<ListViewItem>().Select(l => l.SubItems[0].Text).Where(text => text != null).ToList();

            if (cboPatientID.SelectedIndex == -1) return;
            var id = Int32.Parse(cboPatientID.SelectedValue.ToString());
            _benhNhan = _benhNhanServices.GetBenhNhan(id);
            if (chkCachLi.Checked == true)
            {
                _benhNhan.MucDo = "Tạm cách li";
                _benhNhanServices.EditBenhNhan(_benhNhan);
            }

            donThuoc.MaBn = _benhNhan.MaBn;
            //donThuoc.BenhNhan = _benhNhan;

            if (rtfNote != null) donThuoc.DanDo = rtfNote.Text;
            if (rtfChuanDoan != null) donThuoc.ChuanDoan = rtfChuanDoan.Text;
            donThuoc.NgayKham = DateTime.Now.Date.ToString();
            if (rdbTaiKham.Checked)
            {
                if (dtpNgayTaiKham.Value >= DateTime.Now.Date)
                {
                    donThuoc.NgayTaiKham = dtpNgayTaiKham.Value.ToString();
                }
                else
                {
                    MessageBox.Show(@"Ngày tái khám không hợp lệ");
                }
            }

            StringBuilder tenThuocBuilder = new StringBuilder();
            int count = 0;
            foreach (var str in tenThuocList)
            {
                count++;
                string s = count.ToString() + "/ " + str;
                tenThuocBuilder.AppendLine(s);
            }
            donThuoc.MaThuoc = tenThuocBuilder.ToString();
            StringBuilder soLuongBuilder = new StringBuilder();
            foreach (var str in soLuongList)
            {
                soLuongBuilder.AppendLine(str);
            }
            donThuoc.SoLuong = soLuongBuilder.ToString();
            donThuoc.TenBacSi = _nhanVien.Ten;
            bool addSuccess = donThuocServices.Add(donThuoc);
            if (addSuccess)
            {
                MessageBox.Show(@"Đã thêm vào lịch sử khám bệnh");
                //this.Close();
                _frmMain.dvgBenhAn.DataSource = donThuocServices.GetDonThuocs();

            }
        }

        private void lvwPatientHistory_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void btnXemDonThuoc_Click(object sender, EventArgs e)
        {

            try
            {
                if (lvwPatientHistory.SelectedItems[0] != null)
                {
                    int id = Int32.Parse(lvwPatientHistory.SelectedItems[0].SubItems[0].Text);

                    DonThuocServices donThuocServices = new DonThuocServices();
                    var donThuoc = donThuocServices.GetDonThuoc(id);
                    if (donThuoc != null)
                    {
                        FrmDonThuoc frmDonThuoc = new FrmDonThuoc(donThuoc);
                        frmDonThuoc.Show();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Chưa chọn đơn thuốc");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwThuoc.SelectedItems[0] != null)
                {
                     lvwThuoc.SelectedItems[0].Remove();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Chưa chọn thuốc");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
